package com.kakarote.crm9.erp.jxc.controller;

import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.StrUtil;
import com.jfinal.aop.Inject;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Record;
import com.kakarote.crm9.erp.jxc.entity.ProductInfoEntity;
import com.kakarote.crm9.erp.jxc.service.ProductCategoryService;
import com.kakarote.crm9.erp.jxc.service.ProductInfoService;
import com.kakarote.crm9.utils.BaseUtil;
import com.kakarote.crm9.utils.R;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @Author:Lzq
 * @Date:2021/3/15
 */
public class GoodsCategoryController extends Controller {

    /**
     * 商品详情
     */
    @Inject
    private ProductInfoService productInfoService;

    /**
     * 产品分类
     */
    @Inject
    private ProductCategoryService productCategoryService;

    /**
     * 获取商品详情列表
     */
    public void getProductInfoList(){
        String pageNum = get("pageNum");
        String pageSize = get("pageSize");
        String startNo = String.valueOf((Integer.parseInt(pageNum) - 1) * Integer.parseInt(pageSize));
        if (StrUtil.isNotEmpty(get("name")) || getInt("belongCategory")!=null){
            String name = "";
            Integer belongCategory = null;
            List<ProductInfoEntity> goodsCategoryList = null;
            if (StrUtil.isNotEmpty(get("name"))) name = get("name");
            if (NumberUtil.isValidNumber(getInt("belongCategory"))) belongCategory = getInt("belongCategory");
            Integer total = null;
            if (StrUtil.isNotEmpty(name) && belongCategory == null) {
                goodsCategoryList = productInfoService.getGoodsCategoryListByCondition(name,startNo,pageSize);
                total = productInfoService.getTotal(name);
            }
            if (!StrUtil.isNotEmpty(name) && belongCategory != null) {
                goodsCategoryList = productInfoService.getGoodsCategoryListByCondition(belongCategory,startNo,pageSize);
                total = productInfoService.getTotal(belongCategory);
            }
            if (StrUtil.isNotEmpty(name) && belongCategory != null) {
                goodsCategoryList = productInfoService.getGoodsCategoryListByCondition(name,belongCategory,startNo,pageSize);
                total = productInfoService.getTotal(name,belongCategory);
            }
            renderJson(R.ok().put("data", goodsCategoryList).put("total",total));
        }else {

            List<ProductInfoEntity> goodsCategoryList = productInfoService.getGoodsCategoryList(startNo,pageSize);
            Integer total = null;
            total = productInfoService.getTotal();
            renderJson(R.ok().put("data", goodsCategoryList).put("total",total));
        }
    }
    /**
     * 根据ID获取商品详情列表
     */
    public void getInfoById(){
        Integer id = getInt(0);
        ProductInfoEntity productInfo = productInfoService.getInfoById(id);
        renderJson(R.ok().put("info",productInfo));
    }

    /**
     * 修改品详情
     */
    public void updateProduct(){
        String name = get("name");
        Integer id = getInt("id");
        Integer belongCategory = getInt("belongCategory");
        String belongCategoryName = get("belongCategoryName");
        String price = get("price");
        String unit = get("unit");
        String specification = get("specification");
        ProductInfoEntity productInfo = new ProductInfoEntity();
        productInfo.setId(id);
        if (StrUtil.isNotEmpty(name)) productInfo.setName(name);
        if (NumberUtil.isValidNumber(belongCategory)) productInfo.setBelongCategory(belongCategory);
        if (StrUtil.isNotEmpty(belongCategoryName)) productInfo.setBelongCategoryName(belongCategoryName);
        if (StrUtil.isNotEmpty(price)) productInfo.setBelongCategoryName(belongCategoryName);
        if (StrUtil.isNotEmpty(unit)) productInfo.setBelongCategoryName(belongCategoryName);
        if (StrUtil.isNotEmpty(specification)) productInfo.setBelongCategoryName(belongCategoryName);
        R r = productInfoService.updateProduct(productInfo);
        renderJson(R.ok().put("data",r));
    }
    /**
     * 删除产品
     */
    public void delProduct(){
        String para = getPara(0);
        R r = null;
        if (para.contains(",")) {
            String[] idsStr = para.split(",");
            for (String s : idsStr) {
                r = productInfoService.delProduct(Integer.parseInt(s));
            }
        }else {
            r = productInfoService.delProduct(Integer.parseInt(para));
        }
        renderJson(R.ok().put("data",r));
    }

    /**
     * 新增产品详情
     */
    public void addProduct(){
        String name = get("name");
        Integer belongCategory = getInt("belongCategory");
        String belongCategoryName = get("belongCategoryName");
        String unit = get("unit");
        String price = get("price");
        String specification = get("specification");
        ProductInfoEntity productInfo = new ProductInfoEntity();
        productInfo.setName(name);
        productInfo.setBelongCategory(belongCategory);
        productInfo.setPrice(BigDecimal.valueOf(Double.parseDouble(price)));
        productInfo.setUnit(unit);
        productInfo.setSpecification(specification);
        productInfo.setBelongCategoryName(belongCategoryName);
        productInfo.setCreateBy(BaseUtil.getUser().getUsername());
        productInfo.setCoding(UUID.randomUUID().toString());
        productInfo.setCreateTime(new Date());
        R r = productInfoService.addProduct(productInfo);
        renderJson(r);
    }

    /**
     * 获取商品分类列表
     */
    public void getCategoryList(){
        List<Record> productCategoryList = productCategoryService.getProductCategoryList();
        System.out.println(productCategoryList);
        renderJson(R.ok().put("data", productCategoryList));
    }

    /**
     * 新增商品分类
     */
    public void addCategoryList(){

        String name = get("name");
        renderJson(productCategoryService.insertCategory(name));
    }

    /**
     * 删除商品分类
     */
    public void delCategoryList(){

        Integer id = getParaToInt(0);
        renderJson(productCategoryService.delCategory(id));
    }
}
